package com.tencreat.mapper.mall;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqStringParam;
import com.tencreat.domain.mall.MallOrder;
import com.tencreat.domain.mall.dto.MallSupplierOrderListDTO;
import com.tencreat.domain.mall.vo.*;
import com.tencreat.domain.mall.dto.MallSupplierOrderListVO;
import com.tencreat.domain.pt.vo.PtIncomeVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单Mapper接口
 *
 * @author tencreat
 * @date 2021-08-13
 */
@Repository
public interface MallOrderMapper extends BaseMapper<MallOrder> {

     Page<ResIntegralMallOrderVO> selectIntegralPage(Page page, @Param(Constants.WRAPPER) Wrapper wrapper);

    List<MallOrder> selectOrderList(MallOrder mallOrder);

    List<DateAndDataVO> countNewOrderNum(MallOrder mallOrder);

    Page<MallOrder> page(IPage<MallOrder> page, @Param("params") MallOrder mallOrder);

    /**
     * 供应商-订单列表-订单发货
     * @param page 分页入参
     * @param dto 查询参数
     * @return
     */
    Page<MallSupplierOrderListVO> supplierPage(IPage<MallSupplierOrderListDTO> page, @Param("param") MallSupplierOrderListDTO dto);

    /**
     * 供应商-订单列表-导出
     * @param dto 查询参数
     * @return
     */
    List<MallSupplierOrderListVO> supplierList(@Param("param") MallSupplierOrderListDTO dto);

    Integer getOrderNumDaily(@Param("day") String day,@Param("userId") Long userId);

    List<StatisticsDateVO> getOrderNumDailyV2(@Param("userId") Long userId);

    Integer getUserNumDaily(String day);

    List<StatisticsDateVO> getUserNumDailyV2();//折线图

    BigDecimal selectEarnings(@Param("uuid") Long uuid, @Param("theDayBefore") LocalDate theDayBefore);//拼团

    BigDecimal selectOneselfEarnings(@Param("uuid") Long uuid, @Param("theDayBefore") LocalDate theDayBefore);

    List<PtIncomeVO> selectPeersIncome(@Param("uuid") Long uuid, @Param("theDayBefore") LocalDate theDayBefore);

    /**
     * 某天直推业绩
     * @param uuid
     * @param theDayBefore
     * @return
     */
    List<BigDecimal> getDistribution(@Param("uuid") Long uuid, @Param("theDayBefore") String theDayBefore);

    List<BigDecimal> getDistributionGroup(@Param("uuid") Long uuid, @Param("theDayBefore") LocalDate theDayBefore);
    List<BigDecimal> getDistributionGroupTest(@Param("uuid") Long uuid, @Param("theDayBefore") String time);

    /**
     * 某两个时间段内直推业绩
     * @param startTime
     * @param endTime
     * @param uuid
     * @return
     */
    BigDecimal getDistributionInterval(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, @Param("uuid") Long uuid);

    /**
     * 锁订单
     * @param orderSn 订单号
     * @return 订单对象
     */
    @Select("select * from mall_order where order_sn = #{orderSn} and del_flag = 0 for update")
    MallOrder selectForUpdate(String orderSn);

    @Update("update mall_order set order_status = #{orderStatus} , status_user = #{userStatus}  where order_sn = #{orderSn} and del_flag = 0 ")
    void updateStatus(@Param("userStatus")int userStatus , @Param("orderStatus")int orderStatus ,@Param("orderSn") String orderSn);

    Page<ResOrderGoodsVO> getOrderList(IPage<ResOrderGoodsVO> resOrderGoodsIPage, @Param("id") Long id, @Param("uuid") Long uuid);

    ResOrderGoodsVO getOrderInfo(@Param("idParam") ReqIdParam idParam, @Param("uuid") Long uuid);

    OrderStatisticsVO supplierOrderStatistics(@Param("userId") Long userId);

    BigDecimal getOrderIncomeDaily(@Param("day") String day, @Param("userId") Long userId);

    List<StatisticsDateVO> getOrderIncomeDailyV2(@Param("userId") Long userId);

    List<AdminIndexVo> queryIndexStatistics();

    Integer getCount(String date);

    BigDecimal queryOrderPayPrice(@Param("day") LocalDateTime day, @Param("userId") Long userId,@Param("beginTime") String beginTime,@Param("endTime") LocalDateTime endTime);

    BigDecimal queryOrderOut(@Param("day") LocalDateTime day, @Param("userId") Long userId);

    BigDecimal queryOrderPayPrice2(@Param("day") LocalDateTime day, @Param("userId") Long userId);

    Integer queryOrderCount(@Param("day") LocalDateTime day, @Param("userId") Long userId,@Param("beginTime") String beginTime,@Param("endTime") LocalDateTime endTime);

    /**
     *订单管理数据统计
     * @param type 1|已完成 2|待付款 3|待发货 4|待收货 5|已取消
     * @param day 当天日期
     * @return
     */
    BigDecimal orderStatistics(@Param("type") Integer type, @Param("day") LocalDateTime day,@Param("userId") Long userId,@Param("orderMold") Integer orderMold);

    /**
     * 会员订单金额统计
     * @param type 支付方式
     * @param day 当天日期
     * @return
     */
    BigDecimal memberStatistics(@Param("type") Integer type, @Param("day") LocalDateTime day);

    ResOrderCountVO selectOrderCount(@Param("uuid") Long uuid);//我的订单数量

    Integer buyers(@Param("goodsId") Long goodsId);

    HttpResult<LogisticsVO> logistics(@Param("orderSn") ReqStringParam orderSn);

    HttpResult<AccumulatedVO> accumulatedRecharge(@Param("uuid")Long uuid);

    HttpResult<AccumulatedVO> accumulatedConsumer(@Param("uuid")Long uuid);



    Page<MallOrder> rechargeList(Page<MallOrder> page, @Param("param") MallOrder mallOrder);

    IPage<ResOrderGoodsVO> rechargeRecord(Page<ResOrderGoodsVO> resOrderGoodsIPage, @Param("id") Long id, @Param("uuid") Long uuid);


    IPage<MallOrder> storeOrderList(Page<MallOrder> page, @Param("param") MallOrder mallOrder);


    MallAdminOrderStatisticsVO orderStatistics1(@Param("userId")Long userId, @Param("param")ReqIdParam idParam);

    MallAdminOrderStatisticsVO rechargeOrderStatistics(@Param("userId")Long userId, @Param("param")ReqIdParam idParam);

    MallAdminOrderStatisticsVO orderConsume(@Param("uuid")Long uuid);

    MallAdminOrderStatisticsVO supplierOrder(@Param("userId")Long userId, @Param("param")ReqIdParam idParam);

    MallAdminOrderStatisticsVO storeOrder(@Param("userId")Long userId, @Param("param")ReqIdParam idParam);

    MallAdminOrderStatisticsVO storeOrderStatistics(@Param("userId")Long userId, @Param("param")ReqIdParam idParam);

    BigDecimal storeOrderTotalAmount();

    BigDecimal shopConsumeRoll();

    BigDecimal useConsumeRoll();

}
